import { IOpenScopePluginMenuItem } from "../../../src/libs/interfaces/pluginInterfaces/IOpenScopePluginMenuItem";
import { IPluginInfo } from "../../../src/libs/interfaces/pluginInterfaces/IPluginInfo";
import { IVatsimData } from "../../../src/libs/interfaces/pluginInterfaces/IVatsimData";
import OpenScope from "../../../src/libs/interfaces/OpenScopePluginNamespace";
function init() {
  class ExamplePlugin
    extends OpenScope.OpenScopePluginBase
    implements OpenScope.IOpenScopePluginBase {
    pilotDataRefreshInterval = 0;
    pilotData: IVatsimData | undefined = undefined;
    ws: WebSocket | undefined = undefined;

    refreshPilotData() {
      this.fetch("https://data.vatsim.net/v3/vatsim-data.json")
        .then((data) => {
          data.json().then((pilotData) => {
            this.pilotData = pilotData;
            // This function is called to do something as its name
            this.invokeRadarRedraw();
            console.log('pilot fetched, invokeRadarRedraw', this.pilotData);
          });
        })
        .catch((error) => {
          console.error(error);
        });
    }

    onAcquireMenu(): Array<IOpenScopePluginMenuItem> {
      return [
        {
          text: "Connect to Vatsim-Data",
          action: () => {
            this.refreshPilotData();
            clearInterval(this.pilotDataRefreshInterval);
            this.pilotDataRefreshInterval = setInterval(() => {
              this.refreshPilotData();
            }, 5000);
          },
        },
        {
          text: "Disconnect from Vatsim-Data",
          action: () => {
            clearInterval(this.pilotDataRefreshInterval);
          },
        },
        {
          text: "websocket test",
          action: () => {
            // Of course, you can use the built-in WebSocket Object to open a websocket connection!
            // It's quiet easier than bundle a boost library in your c project, isn't it?
            console.log("websocket start.");
            this.ws = new WebSocket("ws://localhost:4000");
            this.ws.addEventListener("open", (e) => {
              console.log("websocket open");
              this.ws?.send("Hello");
            });
            this.ws.addEventListener("message", (e) => {
              console.log("incoming message from websocket", e);
            });
            this.ws.addEventListener("close", () => {
              console.log("socket close");
            });
            this.ws.addEventListener("error", (e) => {
              console.error(e);
            });
          },
        },
      ];
    }

    onAcquirePluginInformation(): IPluginInfo {
      return {
        pluginName: "Connect And Draw Example",
        authorName: "OpenScope",
        description:
          "This plugin allows you to connect to Vatsim-Data, and draw planes on radar screen.",
      };
    }

    onRadarFrame(ctx: CanvasRenderingContext2D, scaleIndex: number): void {
      if (this.pilotData == undefined) return;
      const renderData = this.getRenderDataInstance();
      for (const pilot of this.pilotData.pilots) {
        ctx.save();
        ctx.translate(
          pilot.longitude * scaleIndex,
          -pilot.latitude * scaleIndex,
        );
        ctx.rotate(pilot.heading * (Math.PI / 180));
        renderData.groundAircraftSymbolDrawer.draw(
          ctx,
          { latitude: 0, longitude: 0 },
          scaleIndex,
        );
        ctx.restore();
      }
    }

    onRenderDataUpdate(_data: OpenScope.RadarRenderData): void { }

    onInitialized(workingDir: string): void {
      console.log("Initialized working directory", workingDir);
    }
  }

  return new ExamplePlugin();
}
